/*
 * @(#)HtmlTextExtractor.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.dms.extractor;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StringReader;
import java.io.StringWriter;

import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.dcivision.framework.CharsetDetector;


/**
  HtmlTextExtractor.java

  This class is the text extractor for HTML files.

    @author          Rollo Chan
    @company         DCIVision Limited
    @creation date   26/08/2003
    @version         $Revision: 1.10.2.1 $
*/

public class HtmlTextExtractor extends AbstractTextExtractor {

  public static final String REVISION = "$Revision: 1.10.2.1 $";
  
  private static final Log log = LogFactory.getLog(HtmlTextExtractor.class);
  
  public HtmlTextExtractor(InputStream fis, String encoding) {
    super(fis, encoding);
    if (fis==null) {
      return;
    }
    BufferedReader reader = null;
    StringWriter writer = null;
    PrintWriter printer = null;
    StringReader inputReader = null;
    try {
      reader = new BufferedReader(new InputStreamReader(fis,"UTF-8"));
      writer = new StringWriter();
      printer= new PrintWriter(writer);
      String line = "";
      while ((line = reader.readLine()) != null) {
        line = line.replaceAll("&nbsp;", "");
        printer.println(line);
      }

       inputReader = new StringReader(writer.toString());
      //DefaultStyledDocument dsd = new DefaultStyledDocument();
      HTMLDocument dsd = new HTMLDocument();
      
      HTMLEditorKit htmlEditorKit = new HTMLEditorKit();
      dsd.putProperty("IgnoreCharsetDirective", new Boolean(true));
     
      htmlEditorKit.read(inputReader, dsd, 0);
      
      text = dsd.getText(0, dsd.getLength());
      InputStream in = new ByteArrayInputStream(text.getBytes());
      String charsetEncoding = CharsetDetector.getCharset(in);
      text = new String (text.getBytes(),charsetEncoding);

    } catch (Exception e) {
      log.error(e, e);
    } finally {
      try { reader.close(); } catch (Exception ignore) {} finally { reader = null; }
      try { writer.close(); } catch (Exception ignore) {} finally { writer = null; }
      try { printer.close(); } catch (Exception ignore) {} finally { printer = null; }
      try { inputReader.close(); } catch (Exception ignore) {} finally { inputReader = null; }
    }
  }

}